package com.alogic.xscript.plugins;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.alogic.xscript.AbstractLogiclet;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;

/**
 * 字符串形式的数组
 * 
 * @author duanyy
 * @since 1.6.12.20 [20190130 duanyy]
 *
 * @version 1.6.15.2 [20211103 duanyy] <br>
 * - 增加缺省值输出 <br>
 */
public class StringList extends Segment {
	protected String $id = "";
	protected String cid = "$stringlist";
	protected String delimeter = ",";
	protected String $dft = NULL;

	public StringList(String tag, Logiclet p) {
		super(tag, p);
		
		registerModule("strlist-add",Add.class);
	}

	@Override
	public void configure(Properties p){
		super.configure(p);
		$id = PropertiesConstants.getRaw(p,"id",$id);
		cid = PropertiesConstants.getString(p,"cid",cid,true);
		delimeter = PropertiesConstants.getString(p,"delimeter",delimeter,true);
		$dft = PropertiesConstants.getRaw(p,"dft",$dft);
	}

	@Override
	protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
		String id = PropertiesConstants.transform(ctx, $id, "");
		if (StringUtils.isNotEmpty(id)){
			//只有定义了输出id才有效
			
			List<String> list = new ArrayList<String>();
			try {
				ctx.setObject(cid, list);				
				super.onExecute(root, current, ctx, watcher);
			}finally{
				ctx.removeObject(cid);
				if (list.isEmpty()) {
					String dft = PropertiesConstants.transform(ctx,$dft,NULL);
					if (StringUtils.isNotEmpty(dft)){
						ctx.SetValue(id,dft);
					}
				}else{
					ctx.SetValue(id, StringUtils.join(list.toArray(new String[list.size()]), delimeter));
				}
			}
		}
	}	
	
	/**
	 * 向数组中增加值
	 * 
	 * @author duanyy
	 *
	 */
	public static class Add extends AbstractLogiclet {
		protected String pid = "$stringlist";
		protected String $value = "";
		protected boolean raw = false;
		protected boolean ref = false;
		
		public Add(String tag, Logiclet p) {
			super(tag, p);
		}

		@Override
		public void configure(Properties p){
			super.configure(p);
			pid = PropertiesConstants.getString(p,"pid",pid,true);
			$value = PropertiesConstants.getRaw(p,"value",$value);
			raw = PropertiesConstants.getBoolean(p, "raw", raw,true);
			ref = PropertiesConstants.getBoolean(p, "ref", ref,true);
		}

		@Override
		protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {	
			List<String> list = ctx.getObject(pid);
			if (list != null){
				if (raw){
					if (ref){
						String v = PropertiesConstants.getRaw(ctx, $value, "");
						if (StringUtils.isNotEmpty(v)){
							list.add(v);
						}
					}else{
						list.add($value);
					}
				}else{
					String v = ref?PropertiesConstants.getString(ctx,$value,"",false):PropertiesConstants.transform(ctx, $value, "");
					if (StringUtils.isNotEmpty(v)){
						list.add(v);
					}
				}
			}
		}	
	}	
}
